package org.gitos.cloud.util;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.util.Base64;
import java.util.HashMap;
import java.util.UUID;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;

public class CommonKit {
	private final static String ALGORITHM = "AES";
	public final static String SALT = "DE7FF5A05A374701BEEF17020C00A22F";
	
	public static String encodePara(HashMap<String, String> map) {
		StringBuffer sb = new StringBuffer();
		try {
			for (String key : map.keySet()) {
				sb.append("&").append(key).append("=")
						.append(URLEncoder.encode(map.get(key), "utf-8"));
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		return sb.toString().replaceFirst("&", "");
	}

	public static String httpReauest(String rqRrl) {
		HttpURLConnection con = null;
		try {
			URL url = new URL(rqRrl);
			con = (HttpURLConnection) url.openConnection();
			con.setRequestMethod("POST");
			con.setConnectTimeout(20000);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					con.getInputStream()));

			String line, returnjson = null;
			while ((line = in.readLine()) != null) {
				System.out.printf(line);
				returnjson += line;
			}
			return returnjson;
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String createUuid() {
		return UUID.randomUUID().toString().replace("-", "");
	}

	public final static String md5(String s) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
				'A', 'B', 'C', 'D', 'E', 'F' };
		try {
			byte[] btInput = s.getBytes();
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			mdInst.update(btInput);
			byte[] md = mdInst.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String encryptAndBase64(String str) {
		byte[] key = SALT.getBytes();
		try {
			SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE, skeySpec);

			byte[] encrypted = cipher.doFinal(str.getBytes());
			return Base64.getEncoder().encodeToString(encrypted);
		} catch (Exception e) {
			return null;
		}
	}

	public static String decodebase64AndDecrypt(String str) {
		try {
			byte[] tmp = Base64.getDecoder().decode(str);
			byte[] key = SALT.getBytes();
			return decrypt(tmp, key);
		} catch (Exception e) {
			return "";
		}
	}

	private static String decrypt(byte[] encrypted, byte[] rawKey) {
		try {
			byte[] tmp = encrypted;
			byte[] key = rawKey;
			SecretKeySpec skeySpec = new SecretKeySpec(key, ALGORITHM);
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.DECRYPT_MODE, skeySpec);
			byte[] decrypted = cipher.doFinal(tmp);
			return new String(decrypted);
		} catch (Exception e) {
			return "";
		}
	}

	public static byte[]  fileToByteArray(File file){
		byte[] buff=null;
		try {
			FileInputStream fis=new FileInputStream(file);
			ByteArrayOutputStream bos=new ByteArrayOutputStream();
			int fileSize = fis.available();
			buff=new byte[fileSize];
			fis.read(buff);
		} catch (Exception e) {
			e.printStackTrace();
			return buff;
		}
		return buff;
	}

}
